package com.zprmb.udzt.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.yintong.secure.widget.Progress;
import com.zprmb.udzt.BbtApplication;
import com.zprmb.udzt.R;
import com.zprmb.udzt.bean.IndexBean;
import com.zprmb.udzt.util.BitmapUtils;
import com.zprmb.udzt.util.Regular;

public class Index_adapter extends BaseAdapter {
    private List<IndexBean> list;
    private Context context;
    DecimalFormat df = new DecimalFormat("###0.00");
    private boolean myflag = false;

    public Index_adapter(List<IndexBean> list, Context context) {
        super();
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder h = null;
        if (convertView == null) {
            h = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.v6_ud_item, null);
            h.tv_name = (TextView) convertView.findViewById(R.id.conduct_name);
            h.tv_time = (TextView) convertView.findViewById(R.id.conduct_time);
            h.tv_rate = (TextView) convertView.findViewById(R.id.conduct_rate);
            h.tv_rateadd = (TextView) convertView.findViewById(R.id.home_rateadd);
            h.tv_investAmount = (TextView) convertView.findViewById(R.id.conduct_investAmount);
            h.v6_ud_invest_increaseNum = (TextView) convertView.findViewById(R.id.v6_ud_invest_increaseNum);
            h.img_list_status = (ImageView) convertView.findViewById(R.id.img_list_status);
            h.li_jiaxi = (LinearLayout) convertView.findViewById(R.id.li_jiaxi);
            h.tv_pl_reward = (TextView) convertView.findViewById(R.id.tv_pl_reward);
            h.img_pl_reward = (ImageView) convertView.findViewById(R.id.img_pl_reward);
            h.sk_progresss = (ProgressBar) convertView.findViewById(R.id.sk_progresss);
            convertView.setTag(h);
        } else {
            h = (ViewHolder) convertView.getTag();
        }
        IndexBean uplan = list.get(position);


        if (uplan.getAssetLoanStatus().equals("COLLECTING")) {
            h.img_list_status.setVisibility(View.INVISIBLE);
        } else if (uplan.getAssetLoanStatus().equals("SETTLEMENTED") || uplan.getAssetLoanStatus().equals("SETTLEMENTED")) {
            h.img_list_status.setVisibility(View.VISIBLE);
            h.img_list_status.setImageBitmap(BitmapUtils.readBitMap(context, R.drawable.img_list_shouyiz));
        } else if (uplan.getAssetLoanStatus().equals("REPAYED")) {
            h.img_list_status.setVisibility(View.VISIBLE);
            h.img_list_status.setImageBitmap(BitmapUtils.readBitMap(context, R.drawable.img_list_back_money));
        } else {
            h.img_list_status.setVisibility(View.VISIBLE);
            h.img_list_status.setImageBitmap(BitmapUtils.readBitMap(context, R.drawable.img_list_finsh));
        }
        h.tv_rate.setText(Regular.getDecimalFormatTwo(Double.parseDouble(list.get(position).getRate()) - Double.parseDouble(list.get(position).getRateAdd())));
        if (Double.parseDouble(list.get(position).getRateAdd()) > 0) {
            h.li_jiaxi.setVisibility(View.VISIBLE);
            h.tv_rateadd.setText(Regular.getDecimalFormatTwo(Double.parseDouble(list.get(position).getRateAdd())) + "%");
        } else {
            h.li_jiaxi.setVisibility(View.GONE);
        }
//		if(list.get(position).getFlagNowEffective()!=null){
//		}
       /* long t1 = System.currentTimeMillis();
        Log.d("3333333", t1 + "");*/
        if (!list.get(position).getFlagNowEffective()) {
            h.tv_pl_reward.setVisibility(View.GONE);
            if ("nonArrivalOpenTime".equals(list.get(position).getNonArrivalOpenTimeStatus())) {
                h.tv_pl_reward.setVisibility(View.VISIBLE);
                h.tv_pl_reward.setText(Regular.getMilliToHour_Min(list.get(position).getOpenTime()) + "开放");
                h.img_pl_reward.setVisibility(View.GONE);
            } else {

                if ("true".equals(uplan.getFlagFresh())) {
                    BbtApplication.getInstance().display2(uplan.getFreshPictureAddress(), h.img_pl_reward);
                    h.img_pl_reward.setVisibility(View.VISIBLE);
                } else {
                    if (uplan.isFlagRestival()==true){
//                        h.img_pl_reward.setImageResource(R.drawable.chongyanng);

                        BbtApplication.getInstance().display2(uplan.getFlagRestivalAddress(), h.img_pl_reward);
                    }else {
                        h.img_pl_reward.setVisibility(View.GONE);
                    }



                }
            }
        } else {
            if ("nonArrivalOpenTime".equals(list.get(position).getNonArrivalOpenTimeStatus())) {
                h.tv_pl_reward.setVisibility(View.VISIBLE);
                h.tv_pl_reward.setText(Regular.getMilliToHour_Min(list.get(position).getOpenTime()) + "开放");
//                h.img_pl_reward.setVisibility(View.GONE);

                if (uplan.isFlagRestival()==true){
//                        h.img_pl_reward.setImageResource(R.drawable.chongyanng);

                    BbtApplication.getInstance().display2(uplan.getFlagRestivalAddress(), h.img_pl_reward);
                }else {
                    h.img_pl_reward.setVisibility(View.GONE);
                }
                /*  if (uplan.getFlagRaiseInterest()==true){
//                    h.img_pl_reward.setImageResource(R.drawable.chongyanng);
                }else {
                    h.img_pl_reward.setVisibility(View.GONE);
                }*/
            } else {
                if ("true".equals(uplan.getFlagFresh())) {
                    h.tv_pl_reward.setVisibility(View.GONE);
                    h.img_pl_reward.setVisibility(View.VISIBLE);
                    BbtApplication.getInstance().display2(uplan.getFreshPictureAddress(), h.img_pl_reward);
                } else {
                    h.tv_pl_reward.setVisibility(View.VISIBLE);
                    h.tv_pl_reward.setText("平台奖励");
//                    h.img_pl_reward.setVisibility(View.GONE);
                    if (uplan.isFlagRestival()==true){
//                        h.img_pl_reward.setImageResource(R.drawable.chongyanng);

                        BbtApplication.getInstance().display2(uplan.getFlagRestivalAddress(), h.img_pl_reward);
                    }else {
                        h.img_pl_reward.setVisibility(View.GONE);
                    }
                }

            }

        }

        h.sk_progresss.setProgress((int) (uplan.getDegree() * 10));
        h.tv_name.setText(list.get(position).getProductName());
        h.tv_investAmount.setText(Regular.getDecimalFormatTwo(Double.parseDouble(list.get(position).getRemainAmount()) / 10000));
        h.tv_time.setText(list.get(position).getProductPeriod());
        h.v6_ud_invest_increaseNum.setText(Regular.getDecimalFormatZero(Double.parseDouble(list.get(position).getMinAmount())) + "元起投");
        return convertView;
    }

    private class ViewHolder {
        private TextView tv_name, tv_coupon, tv_rate, tv_rateadd, tv_time, tv_investAmount, tv_newuser, tv_coupon_type, newuser;
        private LinearLayout li_jiaxi;
        private Button btn_invest;
        private ProgressBar sk_progresss;
        private TextView tv_pl_reward, v6_ud_invest_increaseNum;
        private ImageView img_group, img_pl_reward;
        private ImageView img_list_status;
    }

}
